package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"mall-api-gin/models"
	"mall-api-gin/pkg/gredis"
	"mall-api-gin/pkg/logging"
	"mall-api-gin/pkg/setting"
	"mall-api-gin/pkg/util"
	"mall-api-gin/routers"
	"net/http"
)

func init() {
	setting.SetupConfig()
	logging.Setup()
	util.Setup()
	models.SetupDb()
	gredis.Setup()
}

func main() {
	r := gin.Default()

	routersInit := routers.RouterInit(r)
	readTimeout := setting.ServerSetting.ReadTimeout
	writeTimeout := setting.ServerSetting.WriteTimeout
	endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
	maxHeaderBytes := 1 << 20

	server := &http.Server{
		Addr:           endPoint,
		Handler:        routersInit,
		ReadTimeout:    readTimeout,
		WriteTimeout:   writeTimeout,
		MaxHeaderBytes: maxHeaderBytes,
	}
	logging.Info("[info] start http server listening %s", endPoint)

	go func() {
		err := server.ListenAndServe()
		if err != nil {
			logging.Error(err)
		}
	}()
	select {}
}
